package cn.tedu.service;

import cn.tedu.pojo.Car;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@Component//交给Spring容器进行ioc
public class CarServiceImpl implements CarService {

    @Override
    public Car get() {

        Car car = new Car();
        car.setColor("红色");
        car.setName("法拉利");
        car.setPrice(2345667.0);
        return car;
    }

    @Override
    public void add(Car car) {
        System.out.println(car);
    }

    public void updata(Car c) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql:///cgb2104?characterEncoding=utf8&useSSL=false";
        Connection conn = DriverManager.getConnection(url, "root", "root");

        String sql = "update car set price=? where name=?";
        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setDouble(1,c.getPrice());
        ps.setString(2, c.getName());

        ps.executeUpdate();
        ps.close();
        conn.close();
    }
}
